<script setup lang="ts">
import { getFileList, getFile } from "@/api/search";

const props: any = defineProps({
  comProps: {
    type: Object,
  },
});
const iframeUrl: any = ref("");
const numPages = ref<any>();

const PreviewContianerRef = ref(null);
const loading = ref(false);
const emit = defineEmits(["close"]);
const handleClose = () => {
  emit("close");
  PreviewContianerRef.value?.toggleDialog(false);
};
const get_file = async (path: any) => {
  if (!path) return handleClose();
  console.log(import.meta.env.VITE_PDF_PATH);

  try {
    let res: any = await getFile({
      filePath: path,
    });
    if (res) {
      PreviewContianerRef.value?.click_full();
      setTimeout(() => {
        iframeUrl.value =
          import.meta.env.VITE_PDF_PATH + "?file=" + URL.createObjectURL(res);
        loading.value = false;
      }, 1000);
    }
  } catch (err) {
    loading.value = false;
    return "";
  }
};
// console.log("props", props.preview);
watch(
  props.comProps,
  (val) => {
    // console.log("val", val);
    if (val?.filePath) {
      loading.value = true;
      get_file(val.filePath);
    }
  },
  { immediate: true }
);
</script>

<template>
  <PreviewContianer ref="PreviewContianerRef" :handleClose="handleClose">
    <template v-slot="{ clientHeight }">
      <el-scrollbar :style="{ height: `${clientHeight}` }" v-loading="loading">
        <!-- <VueOfficePdf :src="iframeUrl" class="docx-calss" /> -->
        <iframe :src="iframeUrl" width="100%" height="100%"></iframe>
      </el-scrollbar>
    </template>
  </PreviewContianer>
</template>

<style lang="scss" scoped>
:deep(.el-scrollbar__view) {
  height: 100%;
}
</style>
